import 'package:flick_video_player/flick_video_player.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';
import '../widgets/video_related_list_view.dart';

class VideoDetailPage extends StatefulWidget {
  const VideoDetailPage({super.key});

  @override
  State<VideoDetailPage> createState() => _VideoDetailPageState();
}

class _VideoDetailPageState extends State<VideoDetailPage> {
  final int _id = Get.arguments['id'] ?? 0;
  String _videoUrl = Get.arguments['playUrl'] ?? '';

  late FlickManager _flickManager;

  @override
  void initState() {
    super.initState();

    _flickManager = FlickManager(
      videoPlayerController: VideoPlayerController.networkUrl(
        Uri.parse(_videoUrl),
      ),
    );
  }

  @override
  void dispose() {
    _flickManager.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        //surfaceTintColor: Colors.white,
        title: Image.asset(
          'assets/images/eyepetizer.png',
          fit: BoxFit.contain,
          width: 120,
          height: 25,
        ),
        centerTitle: true,
      ),
      body: Column(
        children: [
          ClipRect(
            child: SizedBox(
              width: double.infinity,
              height: 240,
              child: FlickVideoPlayer(flickManager: _flickManager),
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Expanded(
            child: VideoRelatedListView(
              relateId: _id,
              onItemClick: (String videoUrl) {
                _videoUrl = videoUrl;
                _flickManager.handleChangeVideo(
                  VideoPlayerController.networkUrl(
                    Uri.parse(_videoUrl),
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}
